package cn.zhangxin.pethome.system.department.controller;

import cn.zhangxin.pethome.system.department.domain.Department;
import cn.zhangxin.pethome.system.department.query.DepartmentQuery;
import cn.zhangxin.pethome.system.department.service.IDepartmentService;
import cn.zhangxin.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /* 方式1 */
    //@RequestMapping(value = "/list",method = RequestMethod.GET)
    /* 方式2 */
    /* 查询全部的方法 */
    @GetMapping("/list")
    @ApiOperation(value = "获取列表" )
    public AjaxResult findAll(){
        try {
            List<Department> all = departmentService.findAll();
            return AjaxResult.success().setData(all);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /* 查询一个部门的方法 */
    @GetMapping("/{id}")
    /* 通过get请求获得一个id，然后加上@PathVariable */
    @ApiOperation(value = "根据id获得单个部门信息")
    public AjaxResult findOne(@PathVariable Long id){
        try {
            Department one = departmentService.findOne(id);
            return AjaxResult.success().setData(one);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /* 分页查询,高级查询 */
    /* 如果前端用表单提交数据，这里推荐使用post请求 */
    @PostMapping("/pagelist")
    @ApiOperation(value = "分页查询")
    /* 前后端使用json数据交互，使用在参数前加上RequestBody */
    public AjaxResult findQuery(@RequestBody DepartmentQuery departmentQuery){
        try {
            List<Department> byQuery = departmentService.findByQuery(departmentQuery);
            Long total = departmentService.findByQueryCount(departmentQuery);
            System.out.println("没有乱码");
            System.out.println(total);
            return AjaxResult.success().setData(byQuery).setTotal(total);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /* 删除数据 */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据传入的id删除数据")
    public AjaxResult delete(@PathVariable Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            //获得抛出异常
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /* 保存数据 */
    /* 这里路径什么都不要写，因为是Restful风格 */
    @PutMapping
    @ApiOperation(value = "保存数据【如有id为修改，没id为添加】")
    public AjaxResult save(@RequestBody Department department){
        /* 判断id是否为空，如果为空就是添加，否则就是修改 */
        if (department.getId() == null){
            try {
             departmentService.add(department);
             return AjaxResult.success().setMsg("添加成功");
            }catch (Exception e){
                e.printStackTrace();
            }
            return AjaxResult.error().setMsg("添加失败");
        }else {
            try {
                departmentService.update(department);
                return AjaxResult.success().setMsg("修改成功");
            }catch (Exception e){
                e.printStackTrace();
                return AjaxResult.error().setMsg(e.getMessage());
            }

        }
    }

    /*
    批量删除
    PatchMapping 批量专用
    */
    @PatchMapping("/del")
    @ApiOperation(value = "根据id批量删除")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.success().setMsg("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("批量删除失败");
    }

    /*
    返回无限级树
     */
    @GetMapping("/tree")
    @ApiOperation(value = "获得部门级树")
    public List<Department> getTree(){
        return departmentService.getTree();
    }
}